package cn.tedu.lucky.draw.server.job.config;

import cn.tedu.lucky.draw.server.job.processor.ActivityExpirationProcessor;
import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.repository.ActivityRepository;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.data.RepositoryItemReader;
import org.springframework.batch.item.data.RepositoryItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Sort;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


@Configuration
public class ActivityExpirationConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private ActivityRepository activityRepository;

    //定义processor
    @Bean
    public ActivityExpirationProcessor activityExpirationProcessor() {
        return new ActivityExpirationProcessor();
    }

    //定义reader
    @Bean
    public ItemReader<Activity> activityExpirationReader() {
        RepositoryItemReader<Activity> itemReader = new RepositoryItemReader<>();
        itemReader.setRepository(activityRepository);
        itemReader.setMethodName("findByStatusAndEndTimeBefore");
        Map<String, Sort.Direction> sorts = new HashMap<>();
        sorts.put("id", Sort.Direction.ASC);
        itemReader.setSort(sorts);
        itemReader.setArguments(Arrays.asList(1, LocalDateTime.now()));
        return itemReader;
    }

    //定义writer
    @Bean
    public ItemWriter<Activity> activityExpirationWriter() {
        RepositoryItemWriter<Activity> itemWriter = new RepositoryItemWriter<>();
        itemWriter.setRepository(activityRepository);
        itemWriter.setMethodName("save");
        return itemWriter;
    }

    //定义step
    @Bean
    public Step activityExpirationStep() {
        return stepBuilderFactory.get("activityExpirationStep")
                .<Activity, Activity>chunk(100)//定义批次处理条数
                .reader(activityExpirationReader())
                .processor(activityExpirationProcessor())
                .writer(activityExpirationWriter())
                .build();
    }

    //定义job
    @Bean
    public Job activityExpirationJob() {
        return jobBuilderFactory.get("activityExpirationJob")
                .start(activityExpirationStep())
                .incrementer(new RunIdIncrementer())
                .build();
    }

}
